<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* //现有字符串如下 name1与value1为一组参数信息 name1表示为参数名value1表示为值 
        // 1、 从search字符串中 提取出name1参数的值  值为value1
        var search = '?name1=value1&name2=value2';

        
        // 2、封装一个函数可以获取到指定参数的值

        function getUrlParams(key,str){

        }
        getUrlParams('name2',search) */
        function fn(name1) {
            var search = '?name1=value1&name2=value2';
            var arr1 = search.split("?");
            var arr = arr1[1].split("&");
            var obj = {};
            arr.forEach(item => {
                obj[item.split("=")[0]] = item.split("=")[1];
            });
            return obj[name1];
        }
        var num = fn("name1");
        console.log(num);
    </script>
</body>

</html>